{% extends 'layout.html.twig' %}

{% set bodyClass = 'homepage' %}
{% do script(['libs/echo-js.js','defaultbtheme/js/default/index.js']) %}

{% set consultDisplay = true %}

{% block content %}

  <div class="es-row-wrap">

    <div class="homepage-feature homepage-feature-slides ">
      <div class="cycle-pager"></div>
      {{ block_show('default-b:home_top_banner') }}
    </div>

    <ul class="homepage-category-nav">
      {% set categories = data('Categories', {parentId:0, group:'course'}) %}
      {% for category in categories %}
        <li><a
            href="{{ path('course_set_explore', {category:category.code,fliter:{type: 'all',price: 'all',currentLevelId: 'all'},orderBy:'latest'}) }}">{{ category.name }}</a>
        </li>
      {% endfor %}
      <li><a href="{{ path('course_set_explore') }}">{{ '更多'|trans }}</a></li>
    </ul>

    <div class="row row-9-3">
      <div class="col-md-9">

        {% set openCourses = data('OpenCourses',{ count:3, orderBy:'recommendedSeq' }) %}
        {% if openCourses %}
          <div class="es-box">
            <div class="es-box-heading">
              <h2>公开课</h2>
              <a class="pull-right" href="{{ path('open_course_explore') }}">更多&gt;</a>
            </div>
            <div class="es-box-body">
              {{ _self.open_course_grids(openCourses) }}
            </div>
          </div>
        {% endif %}

        {% set classrooms = data('RecommendClassrooms',{'count':6}) %}
        {% if classrooms %}
          <div class="es-box">
            <div class="es-box-heading">
              <a class="pull-right" href="{{ path('classroom_explore') }}">{{ '更多'|trans }}&gt;</a>
              <h2>{{ '推荐%classroomName%'|trans({'%classroomName%': setting('classroom.name')|default('班级'|trans)}) }}</h2>
            </div>
            <div class="es-box-body">
              <ul class="home-class-list clearfix">
                {% for classroom in classrooms %}
                  {% set teachers = classroom.users|default(null) %}
                  {% if loop.index <= 5 %}
                    <li class="class-item">
                      <a class="class-picture-link" href="{{ path('classroom_show', {id:classroom.id}) }}">
                        {{ lazy_img(filepath(classroom.middlePicture, 'classroom.png'), 'img-responsive', classroom.title, 'lazyload_class.png') }}
                      </a>
                      <div class="class-body">
                        <h3 class="class-title">
                          <a href="{{ path('classroom_show', {id:classroom.id}) }}">{{ classroom.title }}</a>
                        </h3>
                        <div class="class-metas">
                          {% if teachers %}
                            <span class="class-teacher">
                              <i class="es-icon es-icon-people"></i>
                              {% for teacher in teachers %}
                                {% if loop.index <= 1 %}
                                  <a class="teacher-nickname mrl" href="{{ path('user_show', {id:teacher.id}) }}">{{ teacher.nickname }}</a>
                                {% endif %}
                              {% endfor %}
                            </span>
                          {% endif %}
                          <span class="class-student hidden-md"><i class="es-icon es-icon-people"></i>{{ classroom.studentNum + classroom.auditorNum }} 人</span>
                          <span class="class-price">
                            {% if setting('coin.coin_enabled') and setting('coin.price_type') == 'Coin' %}
                              {% if classroom.price > 0 %}
                                {{ classroom.price * setting('coin.cash_rate') }} {{ setting('coin.coin_name') }}
                              {% else %}
                                {{ '免费'|trans }}
                              {% endif %}
                            {% else %}
                              {% if classroom.price > 0 %}
                                {{ '%classroomPrice%元'|trans({'%classroomPrice%': classroom.price}) }}
                              {% else %}
                                {{ '免费'|trans }}
                              {% endif %}
                            {% endif %}
                          </span>
                        </div>
                        <div class="course-num">{{ '共%classroomCourseNum%课程'|trans({'%classroomCourseNum%': classroom.courseNum}) }}</div>
                      </div>
                    </li>
                  {% endif %}
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}


        {# 推荐课程 #}
        {% set recommendCourseSets = data('RecommendCourseSets', {count:3}) %}
        {% if recommendCourseSets %}
          <div class="es-box">
            <div class="es-box-heading">
              <a class="pull-right" href="{{ path('course_set_explore', {sort:'recommendedSeq'}) }}">更多&gt;</a>
              <h2>{{ '推荐课程'|trans }}</h2>
            </div>
            <div class="es-box-body">
              {{ _self.course_grids(data('RecommendCourseSets',{ count:3 })) }}
            </div>
          </div>
        {% endif %}

        {# 最新课程 #}
        {% set latestCourseSets = data('LatestCourseSets', {count:6}) %}
        {% if latestCourseSets %}
          <div class="es-box">
            <div class="es-box-heading">
              <a class="pull-right" href="{{ path('course_set_explore') }}">更多&gt;</a>
              <h2>{{ '最新课程'|trans }}</h2>
            </div>
            <div class="es-box-body">
              {{ _self.course_grids(data('LatestCourseSets',{ count:6 })) }}
            </div>
          </div>
        {% endif %}

        {# 最新直播 #}
        {% if setting('course.live_course_enabled') %}
          {% set recentLiveCourseSets = data('RecentLiveCourseSets', {count:3}) %}
          {% if recentLiveCourseSets %}
            <div class="es-box">
              <div class="es-box-heading">
                <a class="pull-right" href="{{ path('live_course_set_explore') }}">{{ '更多'|trans }}&gt;</a>
                <h2>{{ '最新直播'|trans }}</h2>
              </div>
              <div class="es-box-body">
                {{ _self.course_grids(data('RecentLiveCourseSets',{ count:3 })) }}
              </div>
            </div>
          {% endif %}
        {% endif %}

        {# 最新资讯 #}
        {% set articles = data('LatestArticles',{'count':4}) %}
        {% if articles %}
          <div class="es-box news">
            <div class="es-box-heading">
              <a class="pull-right" href="{{ path('article_show') }}">{{ '更多'|trans }}&gt;</a>
              <h2>{{'最新%article_name%'|trans({'%article_name%': setting('article.name')|default('资讯'|trans)})}}</h2>
            </div>
            <div class="es-box-body">
              <ul class="row">
                {% for article in articles %}
                  {% if article %}
                    <li class="col-md-6">
                      <em>{{ article.publishedTime|date('m-d H:i') }} </em>
                      <a href="{{ path('article_detail', {id:article.id}) }}" title="{{ article.title }}"> {% if article.categoryId %}
                        <span>[{{ article.category.name }}]{% endif %}</span>{{ article.title }} </a>
                    </li>
                  {% endif %}
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {# 推荐话题 #}
        {# {% set threads = data('EliteCourseThreadsByType',{'count':4, 'type':'discussion'}) %}
        {% if threads %}
          <div class="es-box topic">
            <div class="es-box-heading">
              <h2>{{'课程讨论'|trans}}</h2>
            </div>
            <div class="es-box-body">
              <ul class="row">
                {% for thread in threads %}
                  {% if thread %}
                    <li class="col-md-6">
                      <div class="topic-item">
                        <h4>
                          <a href="{{ path('course_thread_show', {courseId:thread.courseId, threadId:thread.id}) }}">{{ thread.title|plain_text(28) }}</a>
                        </h4>
                        <div class="topic-body">
                          {% set user = thread.user %}
                          {% set latestPostUser = thread.latestPostUser %}
                          <img src="{{ filepath(thread.user.mediumAvatar, 'avatar.png') }}" class="pull-left">
                          <h3>{{ user.nickname }}</h3>
                          <p><a href="{{ path('course_show', {id:thread.course.id}) }}">{{ thread.course.title }}</a></p>
                        </div>

                        <p class="topic-bottom">
                          <span>{{ web_macro.user_link(latestPostUser) }}  </span>
              {{ thread.latestPostTime|smart_time }}{{'回复'|trans}}
                          <span class="pull-right">{{'共%threadPostNum%个回复'|trans({'%threadPostNum%': thread.postNum})}}</span>
                        </p>
                      </div>
                    </li>
                  {% endif %} 
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %} #}

        {# 讲师团 #}
        {% set teachers = data('RecommendTeachers',{'count':'3'}) %}
        {% if teachers %}
          <div class="es-box teachers">
            <div class="es-box-heading">
              <h2>{{ '讲师团'|trans }}</h2>
              <a class="pull-right" href="{{ path('teacher') }}">{{ '更多'|trans }}&gt;</a>
            </div>
            <div class="es-box-body">
              <ul class="row teacher-grids">
                {% for teacher in teachers %}
                  {% if teacher %}
                    <li class="col-xs-6  col-sm-6 col-md-4">
                      <div class="teachers-item">
                        <a href="{{ path('user_show', {id:teacher.id}) }}">
                          <img class="img-circle" src="{{ filepath(teacher.mediumAvatar, 'avatar.png') }}">
                        </a>
                        <span>{{ web_macro.user_link(teacher) }}</span>
                        <i> {{ teacher.title }}</i>
                        <p> {{ teacher.about|plain_text(50) }}</p>
                      </div>
                    </li>
                  {% endif %}
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {# vip #}
        {% if is_plugin_installed('Vip') and setting('vip.enabled') %}
          {% set levels = data('VipLevels',{'count':100}) %}
          {% if levels %}
            <div class="es-box vip">
              <div class="es-box-heading">
                <a class="pull-right" href="{{ path('vip') }}">{{ '更多'|trans }}&gt;</a>
                <h2>{{ '会员专区'|trans }}</h2>
              </div>
              <div class="es-box-body">
                <ul class="vip-lists">
                  {% for level in levels %}
                    {% if level %}
                      <li class="vip-item">
                        <div class="row">
                          <div class="col-sm-3">
                            <a href="{{ path('vip_course_explore', {levelId:level.id}) }}">
                              <img class="img-responsive" src="{{ level.icon|default(asset('assets/img/default/vip.png')) }}"
                                alt="{{ level.name }}">
                            </a>
                          </div>
                          <div class="col-sm-6">
                            <h3>{{ level.name }}</h3>
                            <p>{{ level.description|plain_text(40) }}</p>
                          </div>
                          <div class="vip-price col-sm-3">
                            {% if setting('coin.coin_enabled') and setting('coin.price_type') == 'Coin' %}
                              <h4> {{ set_price((level.monthPrice) * (setting('coin.cash_rate'))) }}{{ setting('coin.coin_name', '虚拟币') }}</h4>
                            {% else %}
                              <h4>¥&nbsp;{{ level.monthPrice }}</h4>
                            {% endif %}
                            <h4><a href="{{ path('vip_buy') }}" class="btn btn-success">立即购买</a></h4>
                          </div>
                        </div>
                      </li>
                    {% endif %}
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endif %}
        {% endif %}

        {# 小组 #}
        {% if setting('group').group_show|default(1) %}
          {% set groups = data('HotGroup', {'count':15}) %}
          {% if groups %}
            <div class="es-box hot-group">
              <div class="es-box-heading"><a href="{{ path('group_search_group') }}" class="pull-right">{{ '更多'|trans }}&gt;</a>
                <h2>{{ '最热小组'|trans }}</h2></div>
              <div class="es-box-body">
                <ul class="list-unstyled">
                  {% for group in groups %}
                    {% if group.status=='open' %}
                      <li class="col-md-4">
                        <div class="panel">
                          <div class="media">
                            <a href="{{ path('group_show',{id:group.id}) }}" title="{{ group.title }}" class="pull-left">
                              {% if group.logo %}
                                <img class="avatar-square-sm" src="{{ file_path(group.logo) }}" alt="{{ group.title }}">
                              {% else %}
                                <img class="avatar-square-sm" src="{{ asset('assets/img/default/group.png') }}" alt="{{ group.title }}">
                              {% endif %}
                            </a>
                            <div class="media-body">
                              <p><a href="{{ path('group_show',{id:group.id}) }}" title="{{ group.title }}">{{ group.title|sub_text(10) }}</a></p>
                              <div class="text-muted text-normal">
                                {{ '%groupMemberNum%个成员'|trans({'%groupMemberNum%': group.memberNum}) }}&nbsp;
                                {{ '%groupThreadNum%个话题'|trans({'%groupThreadNum%': group.threadNum}) }}
                              </div>
                            </div>
                          </div>
                        </div>
                      </li>
                    {% endif %}
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endif %}
        {% endif %}
      </div>

      <div class="col-md-3">
        {# 在学课程 #}
        {% if app.user %}
          {% include 'default/member-recently-learned.html.twig' %}
        {% endif %}

        {# 热门标签 #}
        {% set tags = data('Tags',{'count':5}) %}
        {% if tags %}
          <div class="es-box teachers">
            <div class="es-box-heading">
              <h2>{{ '热门标签'|trans }}</h2>
            </div>
            <div class="es-box-body">
              <div class="tag-list">
                {% for tag in tags %}
                  {% if tag %}
                    <a href="{{ path('tag_show', {name:tag.name}) }}"> {{ tag.name }}</a>
                  {% endif %}
                {% endfor %}
              </div>
            </div>
          </div>
        {% endif %}

        {# 课程排行 #}
        {% set courseSetsRank = data('PopularCourseSets',{'count':'5', 'type':'studentNum'}) %}
        {% if courseSetsRank %}
          <div class="es-box courseSets-rank">
            <div class="es-box-heading">
              <h2>{{ '课程排行'|trans }}</h2>
            </div>
            <div class="es-box-body">
              <ul class="media-list">
                {% for courseSet in courseSetsRank %}
                  {% if courseSet %}
                    <li class="media">
                      <a href="{{ path('course_show', {id: courseSet.defaultCourseId}) }}">
                        <div class="course-item">
                          <img src="{{ filepath(course_set_cover(courseSet, 'large'), 'courseSet.png') }}" class="img-responsive thumb pull-left">
                          <div class="course-title">{{ courseSet.title }}</div>
                        </div>
                      </a>
                    </li>
                  {% endif %}
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {# 学员动态 #}
        {% set learns = data('LatestStatuses', {mode:'simple', count: 5,private:0}) %}
        {% if learns %}
          <div class="es-box status-side">
            <div class="es-box-heading">
              <h2>{% if setting('default.user_name') %}{{ setting('default.user_name')|default('学员'|trans) }}{% else %}{{ '学员'|trans }}{% endif %}{{ '动态'|trans }}</h2>
            </div>
            <div class="es-box-body">
              <ul class="media-list">
                {% for learn in learns %}
                  <li class="media">
                    {{ web_macro.user_avatar(learn.user,'pull-left','media-objec',true) }}
                    <div class="media-body">
                      {{ web_macro.user_link(learn.user) }}
                      {{ learn.message|raw }}
                    </div>
                  </li>
                {% endfor %}
              </ul>
            </div>
          </div>
        {% endif %}

        {{ render(controller('AppBundle:Default:latestReviewsBlock', {number:5})) }}

        {# 最热话题 #}
        {% if setting('group').group_show|default(1) %}
          {% set hotThreads = data('HotThreads', {'count':11}) %}
          {% if hotThreads %}
            <div class="es-box hot-threads">
              <div class="es-box-heading"><h2>{{ '最热话题'|trans }}</h2></div>
              <div class="es-box-body">
                <ul class="text-list">
                  {% for thread in hotThreads %}
                    {% if thread %}
                      <li
                        style="border-bottom:none;background:url('{{ asset('assets/img/default/triangle.png') }}') no-repeat 0 3px;padding-left:8px;padding-top:0px;margin-bottom:8px;">
                        <a href="{{ path('group_thread_show',{id:thread.groupId,threadId:thread.id}) }}">{{ thread.title|sub_text(15) }}</a></li>
                    {% endif %}
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% endif %}
        {% endif %}

      </div>
    </div>

  </div>
{% endblock %}

{# {% block contact %}
  {% if contact %}
  {% if contact.enabled == 1 %}
    {% include 'TopxiaWebBundle:ContactFloat:contact.html.twig'   with {contact:contact} %}
  {% endif %}
  {% endif %}
{% endblock %} #}

{% macro course_grids(courseSets) %}
  {% set mode = mode|default('default') %}
  {% if courseSets %}
    <ul class="course-grids clearfix">
      {% for courseSet in courseSets %}
        {% set course = data('PublishedCourseByCourseSet', {'courseSetId':courseSet.id}) %}
        {% if course %}
          <li class="course-grid">
            <a href="{{ path('course_show', {id: courseSet.defaultCourseId}) }}" class="grid-body">
              {{ lazy_img(filepath(course_set_cover(courseSet, 'large'), 'courseSet.png'), 'img-responsive thumb', '') }}
              {% if courseSet.status == 'draft' %}
                <span class="label  label-warning course-status">{{ '未发布'|trans }}</span>
              {% elseif courseSet.status == 'closed' %}
                <span class="label label-danger course-status">{{ '已关闭'|trans }}</span>
              {% endif %}
              {% if courseSet.serializeMode=='serialize' %}
                <span class="label label-success series-mode-label">{{ '更新中'|trans }}</span>
              {% elseif courseSet.serializeMode=='finished' %}
                <span class="label label-warning series-mode-label">{{ '已完结'|trans }}</span>
              {% endif %}

              {% if courseSet.type == 'live' %}
                {% set lesson = courseSet['lesson']|default(null) %}
                {% if lesson and "now"|date("U") >= lesson.startTime and "now"|date("U") <= lesson.endTime %}
                  <span class="label label-warning series-mode-label">{{ '正在直播中'|trans }}</span>
                {% else %}
                  <span class="label label-success series-mode-label">{{ '直播'|trans }}</span>
                {% endif %}
              {% endif %}

              <span class="title">{{ courseSet.title }}</span>
              {% if mode in ['default', 'teach'] %}

                {% if courseSet.type == 'live' %}
                  {% set lesson = courseSet['lesson']|default(null) %}
                  {% if lesson %}
                    <span class="live-course-lesson metas">
                  <span class="text-success mrm">{{ lesson.startTime|date('m-d H:i') }} ~ {{ lesson.endTime|date('H:i') }}</span>
                  <span class="text-muted mrm">{{ '第%lessonNumber%课时'|trans({'%lessonNumber%': lesson.number}) }}</span>
                </span>
                  {% endif %}
                {% endif %}

                <span class="metas clearfix">
            {% if setting('courseSet.coursesPrice') == 0 %}
              <div class="price-block">
                 {% include "course/widgets/course-set-price.html.twig" %}
              </div>
            {% endif %}

                  <span class="review-col pull-left" style="text-align: left;">
                {% if course.ratingNum > 0 %}
                  <span class="meta-label"><strong>{{ courseSet.ratingNum }}</strong> {{ '评价'|trans }}</span>
                  <span class="review-rating">
                    <span class="stars-{{ (courseSet.rating)|number_format }}">&nbsp;</span>
                  </span>
                {% else %}
                  <span class="meta-label">{{ '无评价'|trans }}</span>
                  <span class="review-rating">
                    <span class="stars-0">&nbsp;</span>
                  </span>
                {% endif %}
              </span>

                  {% if setting('course.show_student_num_enabled', '1') == 1 %}
                    <span class="student-col">
                <span class="meta-label">{{ '学生'|trans }}</span>
                <span class="student-num">{{ '%courseTtudentNum%人'|trans({'%courseTtudentNum%': courseSet.studentNum}) }}</span>
              </span>
                  {% endif %}

            </span>
              {% endif %}

              {% if mode in ['default'] %}
                {% set user = users[course.teacherIds|first]|default(null) %}
                {% if user %}
                  <span class="teacher clearfix">
                <img src="{{ filepath(user.smallAvatar, 'avatar.png') }}" class="thumb">
                <span class="nickname ellipsis">{{ user.nickname }}</span>
                <span class="user-title ellipsis">{{ user.title }}</span>
              </span>
                {% endif %}
              {% endif %}

              {% if mode in ['learn'] %}
                <div class="learn-status">
                  {% if courseSet.memberIsLearned %}
                    <div class="progress">
                      <div class="progress-bar progress-bar-success" style="width: 100%;"></div>
                    </div>
                    <div class="action"><span class="btn btn-default btn-sm">{{ '查看课程'|trans }}</span></div>
                  {% else %}
                    <div class="progress">
                      <div class="progress-bar progress-bar-success" style="width: {{ percent(course.memberLearnedNum, course.lessonNum) }};"></div>
                    </div>
                    <div class="action"><span class="btn btn-primary btn-sm">{{ '继续学习'|trans }}</span></div>
                  {% endif %}
                </div>
                {% if courseSet.status == 'draft' %}
                  <span class="label  label-warning course-status">{{ '未发布'|trans }}</span>
                {% elseif courseSet.status == 'closed' %}
                  <span class="label label-danger course-status">{{ '已关闭'|trans }}</span>
                {% endif %}
              {% endif %}

              {% if mode == 'teach' %}
                {% if courseSet.status == 'published' %}
                  <span class="label label-success course-status">{{ '已发布'|trans }}</span>
                {% elseif courseSet.status == 'draft' %}
                  <span class="label  label-warning course-status">{{ '未发布'|trans }}</span>
                {% elseif courseSet.status == 'closed' %}
                  <span class="label label-danger course-status">{{ '已关闭'|trans }}</span>
                {% endif %}
              {% endif %}

            </a>
          </li>
        {% endif %}
      {% endfor %}
    </ul>
  {% else %}
    <div class="empty">{{ '暂无课程。'|trans }}</div>
  {% endif %}
{% endmacro %}

{% macro open_course_grids(courses) %}
  {% if courses %}
    <ul class="course-grids clearfix">
      {% for course in courses %}
        {% set lessons = data('OpenCourseLessons',{courseId:course.id,count:1}) %}

        <li class="course-grid">
          <a href="{{ path('open_course_show', {courseId:course.id}) }}" class="grid-body">
            {{ lazy_img(filepath(course.largePicture, 'course.png'), 'img-responsive thumb', '') }}

            {% if course.type == 'liveOpen' %}
              {% if lessons and "now"|date("U") >= lessons[0].startTime and "now"|date("U") <= lessons[0].endTime %}
                <span class="label label-warning series-mode-label">正在直播中</span>
              {% else %}
                <span class="label label-success series-mode-label">直播</span>
              {% endif %}
            {% endif %}

            <span class="title">{{ course.title }}</span>

            {#{% if course.type == 'liveOpen' and lessons %}
              <span class="live-course-lesson metas">
                <span class="text-success mrm">{{ lessons[0].startTime|date('n月j日 H:i') }} ~ {{ lessons[0].endTime|date('H:i') }}</span>
              </span>
            {% endif %}#}

            <span class="metas clearfix">
                <span class="text-muted mrm">
                  <i class="es-icon es-icon-removeredeye mrs"></i>{{ course.hitNum }}
                </span>
                <span class="text-muted mrm">
                  <i class="es-icon es-icon-thumbup mrs"></i>{{ course.likeNum }}
                </span>
                <span class="text-muted mrm">
                  <i class="es-icon es-icon-textsms mrs"></i>{{ course.postNum }}
                </span>
              </span>

            {% set user = users[course.teacherIds|first]|default(null) %}
            {% if user %}
              <span class="teacher clearfix">
                <img src="{{ filepath(user.smallAvatar, 'avatar.png') }}" class="thumb">
                <span class="nickname ellipsis">{{ user.nickname }}</span>
                <span class="user-title ellipsis">{{ user.title }}</span>
              </span>
            {% endif %}

          </a>
        </li>
      {% endfor %}
    </ul>
  {% else %}
    <div class="empty">暂无课程。</div>
  {% endif %}
{% endmacro %}

